<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodList" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goodList" style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80px"></el-table-column>
        <el-table-column label="创建时间" width="160px">
            <template slot-scope="scoped">
                {{ scoped.row.add_time | dataForm}}
            </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scoped">
              <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="ListDelete(scoped.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
export default {
  data () {
    return {
      queryInfo: {
        //  查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 显示条数
        pagesize: 5
      },
      total: 0,
      //   数据列表
      goodList: []
    }
  },
  created () {
    this.getGoodList()
  },
  methods: {
    //   获取全部数据列表
    async getGoodList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      this.goodList = res.data.goods
      this.total = res.data.total
      // console.log(res.data)
    },
    handleSizeChange (page) {
      this.queryInfo.pagesize = page
      console.log(222)

      this.getGoodList()
    },
    handleCurrentChange (size) {
      this.queryInfo.pagenum = size
      console.log(111)
      this.getGoodList()
    },
    // 删除按钮
    ListDelete (id) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async item => {
        if (item === 'confirm') {
          const { data: res } = await this.$http.delete('goods/' + id)
          console.log(res)
          if (res.meta.status !== 200) return
          console.log('成功')
          this.getGoodList()
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.el-pagination {
    margin-top: 15px;
}
/deep/ .el-card {
    height: 450px;
}
</style>
